package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "订单接口")
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 添加订单信息
     * @return
     */
    @ApiOperation("添加订单信息")
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {

        log.info("添加订单信息 orders = {}", orders);

        ordersService.submit(orders);

        return R.success();
    }


    /**
     * 移动端，分页查询订单信息
     */
    @ApiOperation("移动端，分页查询订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataTypeClass = Integer.class)
    })
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> orderPage(Integer page,Integer pageSize) {
        log.info("分页查询订单 page = {}, pageSize = {}", page, pageSize);

        // 1. 组装分页查询
        Page<Orders> pageInfo = new Page<>(page,pageSize);

        // 2. 执行分页查询,需要根据下单时间排个序
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,ordersLambdaQueryWrapper);

        // 3. 完善数据，根据订单表，获取订单明细数据
        Page<OrdersDto> ordersDtoPage = new Page<>();

        List<OrdersDto> ordersDtoList = pageInfo.getRecords().stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);

            LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OrderDetail::getOrderId, orders.getId());
            List<OrderDetail> orderDetailList = orderDetailService.list(lambdaQueryWrapper);

            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtoList);

        return R.success(ordersDtoPage);
    }


    /**
     * 服务端，分页查询
     */
    @ApiOperation("服务端，分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "beginTime",value = "开始日期", dataTypeClass = LocalDateTime.class),
            @ApiImplicitParam(name = "endTime",value = "结束日期", dataTypeClass = LocalDateTime.class)
    })
    @GetMapping("/page")
    public R<Page<Orders>> page(@RequestParam(required = false, defaultValue = "1") Integer page,
                                @RequestParam(required = false, defaultValue = "5") Integer pageSize, String number,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){

        // 1. 组装分页查询条件
        Page<Orders> pageInfo = new Page<>(page,pageSize);

        // 2. 组装查询条件
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(number), Orders::getNumber, number);
        // 时间区间
        lambdaQueryWrapper.between(beginTime != null,Orders::getOrderTime,beginTime,endTime);
        lambdaQueryWrapper.orderByDesc(Orders::getOrderTime);

        ordersService.page(pageInfo, lambdaQueryWrapper);

        return R.success(pageInfo);
    }


    /**
     * 修改订单的状态
     * @param orders
     * @return
     */
    @ApiOperation("修改订单的状态")
    @PutMapping
    public R update(@RequestBody Orders orders) {
        log.info("orders = {}", orders);

        // 修改订单的状态
        ordersService.updateById(orders);

        return R.success();
    }


    /**
     * 在来一单的业务
     *   根据订单id查询出，订单明细信息，在将明细信息添加到购物车中
     * @return
     */
    @ApiOperation("在来一单的业务")
    @PostMapping("/again")
    public R again(@RequestBody Orders orders) {
        log.info("订单对象  orders = {}", orders);

        ordersService.again(orders);

        return R.success();
    }


}
